var element = Z.element;
var Box = element.Box;
var Button = element.Button;

function List() {
	var head = null;
	var tail = null;

	return {
		addItem : function( value ) {
			var node = {
				value : value,
				next : null,
				previous : tail
			}; // 硬上下文

			if ( tail !== null ) {
				tail.next = node;
			}
			else {
				head = node;
			}
			tail = node;

			return {
				remove : function() {
					// 连接前后
					if ( node.previous !== null ) {
						node.previous.next = node.next;
					}
					else {
						head = node.next;
					}

					if ( node.next !== null ) {
						node.next.previous = node.previous;
					}
					else {
						// node无后,即最后一个节点
						tail = node.previous;
					}
				},
				insertBefore : function( value ) {
					// 这里有new
					var newNode = {
						previous : node.previous,
						next : node,
						value : value
					};

					node.next = newNode;
					if ( node.previous === null ) {
						head = newNode;
					}
					else {
						node.previous.next = newNode;
					}
				},
				insertAfter : function( value ) {
					// 这里也有new
					var newNode = {
						previous : node,
						next : node.next,
						value : value
					};

					node.previous = newNode;
					if ( node.next === null ) {
						tail = newNode;
					}
					else {
						node.next.previous = newNode;
					}
				}
			};
		}
	};
}

onLoad( function( body ) {
			var list = List(); // 该list的集体删除发生在什么时候?
			var node = list.addItem( 1 );
			list.addItem( 2 );
			list.addItem( 3 );

			var button = null;
			button = Button( body );
			button.onClick( function() {
						node.remove();
						button.remove();
					} );
		} );